<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use think\Db;
use think\Validate;
use cmf\controller\RestUserBaseController;
use app\admin\model\LinkModel;

class SalesmanController extends RestUserBaseController
{


    // api 首页友情连接
    public function link()
    {
        $linkModel = new LinkModel();
        $links     = $linkModel->select()->toArray();

        $this->success("获取友情连接成功!", $links);
    }


    /**
     * 获取新建项目列表
     */
    public function project_type()
    {
        $result = Db::name("project_type")->select()->toArray();

        foreach ($result as &$v) {
            $v['l_id'] = explode(',', $v['l_id']);
        }
        unset($v);

        $this->success('获取成功!', $result);

    }

    // 获取项目表列表的;
    public function index()
    {
      $validate = new Validate([
           'page'   => 'number|between:1,99999999',
       ]);

       $validate->message([
           'page.number'   => '页码必须是数字',
           'page.between'  => '页码只能在1-99999999之间',
       ]);

       $new_data = $this->request->param();
       if (!$validate->check($new_data)) {
           $this->error($validate->getError());
       }

      $userId = $this->getUserId();
      $role = $this->getUserRole();
      if ($role!= '业务员') {
        // code...
        $this->error('请切换业务员登陆');
      }

        $where = '';
        if (!empty($new_data['search'])) {
            $search = $new_data['search'];
            $where['company_name']   = ['like', "%$search%"];
        }


      $project_data = Db::name('project')
          ->alias('p')
          ->join('__PROJECT_TYPE__ t','t.id = p.project_type_id')
          ->join('__USER__ u','p.yewu_id = u.id')
          ->field('p.*, t.name, u.user_nickname')
          ->order('status')
          ->where($where)
          ->select()
          ->toArray();
      empty($project_data)?$this->success('暂无项目!'):'';
      $type = [0 => '申请中', 1=>'分配中', 2=>'修改中', 3=>'审核中', 4=>'已完成', 5=>'审核不通过'];
      foreach ($project_data as $key => &$value) {
        // code...
        $value['status_name'] = $type[$value['status']];
      }
      unset($value);

      $result= array();
      foreach ($project_data as $key => $info) {
          $result[$info['company_name']][] = $info;
      }

//      dump($result);
//      exit;
      //统计项目数量
      $count = count($result);
        if ($count < 10) {
            foreach ($result as $key => &$value) {
                $row = count($value);
                foreach ($value as $k => &$v) {
                    // code...
                    $v['row'] = $row;
                    $row = $row-$row;
                }
            }
            $all['pagedata'] = $result;
            $all['count_all'] = 1;
        } else {
            //获取切割次数
            $offset = ceil(count($result) / 10);

            // 切割数组
            for ($i=0; $i < $offset; $i++) {
                $data[$i] = array_slice($result, $i*10, 10);
            }

            $page = $new_data['page'] - 1;

            foreach ($data[$page] as $key => &$value) {
                $row = count($value);
                foreach ($value as $k => &$v) {
                    // code...
                    $v['row'] = $row;
                    $row = $row-$row;
                }
            }
            $count_all = count($data);
            $all['pagedata'] = $data[$page];
            $all['count_all'] = $count_all;

        }




        $this->success('获取成功!', $all);


    }




}
